package com.aidex.web.controller.api;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.domain.vo.Customer21ListVo;
import com.aidex.biz.domain.vo.Customer21Vo;
import com.aidex.biz.service.CustomerService;
import com.aidex.biz.smy.dto.BaseDTO;
import com.aidex.biz.smy.service.HitService;
import com.aidex.biz.smy.vo.BaseVO;
import com.aidex.biz.utils.AESUtils;
import com.aidex.biz.utils.Rong360Codec;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sign.Md5Utils;
import com.aidex.web.controller.tool.Base64Util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 客户信息推送
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-04-22
 */
@RestController("ApiPushCustomerController")
@RequestMapping("/webApi/pushCustomer")
public class PushCustomerController extends BaseController {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private HitService hitService;

    /**
     * 已接通 1
     *
     * @param customer
     * @return
     */
    @Log(title = "左心房客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertZxf", method = RequestMethod.POST)
    @ResponseBody
    public Map insertZxf(@RequestBody Customer customer) {
        customer.setLaiyuan("左心房");
        String[] area = customer.getCityName().split("-");
        if (area.length == 2) {
            customer.setCityName(area[1]);
        }
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 2 周三对接
     *
     * @param customer
     * @return
     */
    @Log(title = "欧乐客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertOuLe", method = RequestMethod.POST)
    @ResponseBody
    public Map insertOuLe(@RequestBody Customer customer) {
        customer.setLaiyuan("欧乐");
        String name = customer.getCusName();
        customer.setCusName(name.replaceAll("左心房", "").replaceAll("（", "").replaceAll("）", ""));
        return customerService.savePushCustomer(customer);
    }

    /**
     * 接通
     *
     * @param customer
     * @return
     */
    @Log(title = "惠利客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertHuiLi", method = RequestMethod.POST)
    @ResponseBody
    public Map insertHuiLi(@RequestBody Customer customer) {
        customer.setLaiyuan("惠利");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 大鹅客户推送信息(撞库)
     *
     * @param param
     * @return
     */
    @Log(title = "大鹅客户推送信息(撞库)", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public BaseVO hitUser(@RequestBody BaseDTO param) {
        BaseVO baseVO = hitService.handlerHit(param);
        return baseVO;
    }

    /**
     * 已接通 4
     *
     * @param param
     * @return
     */
    @Log(title = "大鹅客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/infoSubmit", method = RequestMethod.POST)
    @ResponseBody
    public BaseVO infoSubmit(@RequestBody BaseDTO param) {
        BaseVO baseVO = hitService.handlerCommit(param);
        return baseVO;
    }


    /**
     * 已接通 5
     *
     * @param customer
     * @return
     */
    @Log(title = "巨匠客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertJujiang", method = RequestMethod.POST)
    @ResponseBody
    public Map insertJujiang(@RequestBody Customer customer) {
        customer.setLaiyuan("巨匠");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 6
     *
     * @param jsonObject
     * @return
     */
    @Log(title = "客哆客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertKeDuo", method = RequestMethod.POST)
    @ResponseBody
    public Map insertKeDuo(@RequestBody JSONObject jsonObject) {
        Customer customer = new Customer();
        customer.setLaiyuan("客哆");
        customer.setCusName(jsonObject.getString("customer_name"));
        customer.setPhone(jsonObject.getString("customer_phone"));
        customer.setSex(jsonObject.getString("customer_sex"));
        customer.setCityName(jsonObject.getString("customer_region"));
        customer.setAmount(jsonObject.getString("loan_money"));
        customer.setRemark(jsonObject.getString("media") + "/" + jsonObject.getString("loan_type"));
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通
     *
     * @param jsonObject
     * @return
     */
    @Log(title = "聚网客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertJuWang", method = RequestMethod.POST)
    @ResponseBody
    public Map insertJuWang(@RequestBody JSONObject jsonObject) {
        Customer customer = new Customer();
        customer.setLaiyuan("JuWang");
        customer.setCusName(jsonObject.getString("cname"));
        customer.setPhone(jsonObject.getString("tel"));
        String sex = "未知";
        if (null != jsonObject.getInteger("sex")) {
            if (jsonObject.getInteger("sex") == 1) {
                sex = "女";
            }
            if (jsonObject.getInteger("sex") == 2) {
                sex = "男";
            }
        }
        customer.setSex(sex);
        customer.setCityName(jsonObject.getString("city"));
        customer.setAmount(jsonObject.getString("xqed"));
        String laiyuan = jsonObject.getString("fromId");
        if ("124".equals(laiyuan)) {
            laiyuan = "百度广告1";
        }
        if ("94".equals(laiyuan)) {
            laiyuan = "百度广告2";
        }
        if ("155".equals(laiyuan)) {
            laiyuan = "抖音广告";
        }
        if ("191".equals(laiyuan)) {
            laiyuan = "腾讯广告";
        }

        customer.setRemark(laiyuan + "/" + jsonObject.getString("remarks"));
        if (null != jsonObject.getInteger("n1")) {
            customer.setAge(jsonObject.getInteger("n1"));
        }
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通
     *
     * @param jsonObject
     * @return
     */
    @Log(title = "聚网客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertJuWangZhiZheng", method = RequestMethod.POST)
    @ResponseBody
    public Map insertJuWangZhiZheng(@RequestBody JSONObject jsonObject) {
        Customer customer = new Customer();
        customer.setLaiyuan("至正");
        customer.setCusName(jsonObject.getString("cname"));
        customer.setPhone(jsonObject.getString("tel"));
        String sex = "未知";
        if (null != jsonObject.getInteger("sex")) {
            if (jsonObject.getInteger("sex") == 1) {
                sex = "女";
            }
            if (jsonObject.getInteger("sex") == 2) {
                sex = "男";
            }
        }
        customer.setSex(sex);
        customer.setCityName(jsonObject.getString("city"));
        customer.setAmount(jsonObject.getString("xqed"));
        String laiyuan = jsonObject.getString("fromId");
        if ("124".equals(laiyuan)) {
            laiyuan = "百度广告1";
        }
        if ("94".equals(laiyuan)) {
            laiyuan = "百度广告2";
        }
        if ("155".equals(laiyuan)) {
            laiyuan = "抖音广告";
        }
        if ("191".equals(laiyuan)) {
            laiyuan = "腾讯广告";
        }


        customer.setRemark(laiyuan + "/" + jsonObject.getString("remarks") + "/城市：" + customer.getCityName());
        if (null != jsonObject.getInteger("n1")) {
            customer.setAge(jsonObject.getInteger("n1"));
        }
        customer.setCityName("长沙");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 8
     *
     * @param customer
     * @return
     */
    @Log(title = "通联沃数客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertLianTongWoShu", method = RequestMethod.POST)
    @ResponseBody
    public Map insertLianTongWoShu(@RequestBody Customer customer) {
        customer.setLaiyuan("通联沃数");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 9
     *
     * @param customer
     * @return
     */
    @Log(title = "优搜客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertYouSou", method = RequestMethod.POST)
    @ResponseBody
    public Map insertYouSou(@RequestBody Customer customer) {
        customer.setLaiyuan("优搜");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomer", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomer(@RequestBody Customer customer) {
        String city = customer.getCityName();
        String citys[] = city.split("-");
        if (citys.length == 2) {
            customer.setCityName(citys[1].replace("市", ""));
        } else {
            customer.setCityName(city.replace("市", ""));
        }
        if ("千橙".equals(customer.getLaiyuan()) || "尘屿微".equals(customer.getLaiyuan())) {
            String oldCity = customer.getCityName();
            customer.setCityName("长沙");
            customer.setRemark(customer.getRemark() + "原城市：" + oldCity);
        }
        // customer.setRemark(customer.getCusBody()+"/"+customer.getCusCanal()+"/"+customer.getRemark());
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "佑人客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerZouRen", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerZouRen(@RequestBody Customer customer) {
//        String city=customer.getCityName();
//        city=city.substring(city.indexOf("省")+2,city.indexOf("市")).replace("市","");
//        customer.setCityName(city);
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "117客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomer117", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomer117(@RequestBody Customer customer) {
        String city = customer.getCityName();
//        city=city.substring(city.indexOf("省")+2,city.indexOf("市")).replace("市","");
        customer.setCityName("长沙");
        customer.setLaiyuan("聚网");
        customer.setRemark(customer.getRemark() + "/" + city + "/" + (StringUtils.isNotNull(customer.getReceiveTime()) ? customer.getReceiveTime() : "-"));
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "zx客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerZx", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerZx(@RequestBody Customer customer) {
        String city = customer.getCityName();
        String citys[] = city.split("-");
        if (citys.length == 2) {
            customer.setCityName(citys[1].replace("市", ""));
        } else {
            customer.setCityName(city.replace("市", ""));
        }
        boolean isChange = true;
        // 长沙，益阳，常德 ，娄底，邵阳，怀化，郴州，永州，湘西，张家界
        String[] cityNames = {"岳阳", "衡阳", "株洲", "湘潭"};
        for (String cityName : cityNames) {
            if (customer.getCityName().contains(cityName)) {
                isChange = false;
                break;
            }
        }
        if (isChange) {
            customer.setRemark(customer.getRemark() + "/原城市：" + customer.getCityName());
            customer.setCityName("长沙");
        }
        if ("湘西土家族苗族自治州".equals(customer.getCityName())) {
            customer.setCityName("湘西");
        }
        customer.setLaiyuan("zx");
        return customerService.savePushCustomer(customer);
    }

    /**
     * 飓峰
     *
     * @param customer
     * @return
     */
    @Log(title = "飓峰客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerJF", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerJF(@RequestBody Customer customer) {
        String city = customer.getCityName();
        String citys[] = city.split("-");
        if (citys.length == 2) {
            customer.setCityName(citys[1].replace("市", ""));
        } else {
            customer.setCityName(city.replace("市", ""));
        }
        boolean isChange = true;
//        // 长沙，益阳，常德 ，娄底，邵阳，怀化，郴州，永州，湘西，张家界
//        String[] cityNames={"长沙","益阳","常德","娄底","邵阳","怀化","郴州","永州","湘西","张家界"};
//        for (String cityName:cityNames){
//            if (cityName.equals(customer.getCityName())){
//                isChange=true;
//                break;
//            }
//        }
        if (isChange) {
            customer.setRemark(customer.getRemark() + "/原城市：" + customer.getCityName());
            customer.setCityName("长沙");
        }
        customer.setLaiyuan("飓峰");
        return customerService.savePushCustomer(customer);
    }


    /**
     * 客户信息撞库查询 11 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "客户信息撞库查询", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/customerCheck", method = RequestMethod.POST)
    @ResponseBody
    public Map customerCheck(@RequestBody Customer customer) {

        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        if (customerService.findCustomerByphone(customer)) {
            map.put("data", false);
            map.put("msg", "客户信息存在");
        } else {
            map.put("data", true);
            map.put("msg", "客户信息不存在");
        }
        return map;
    }

    /**
     * 已接通 10
     *
     * @param param
     * @return
     */
    @Log(title = "客户推送信息-车抵融", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerByCheDiRong", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerByCheDiRong(@RequestBody JSONObject param) throws Exception {

        Map map = new HashMap();
        if (StringUtils.isNotEmpty(param.getString("data"))) {
            String dataRes = AESUtils.Decrypt(param.getString("data"), "kzhtybx8qzmjrc06");
            JSONObject jsonObject = JSONObject.parseObject(dataRes);
            if (null != jsonObject) {
                Customer customer = new Customer();
                customer.setAmount(jsonObject.getString("amount"));
                customer.setCityName(jsonObject.getString("cityName"));
                customer.setStatus(jsonObject.getString("carStatus"));
                customer.setCusName(jsonObject.getString("cusName"));
                customer.setPhone(jsonObject.getString("phone"));
                customer.setLaiyuan("车抵融");
                customer.setCusCanal(jsonObject.getString("dc"));
                customer.setRemark(customer.getCusBody() + "/" + customer.getCusCanal() + "/" + customer.getRemark());
                return customerService.savePushCustomer(customer);
            } else {
                map.put("code", "500");
                map.put("message", "json is error");
            }
        } else {

            map.put("code", "500");
            map.put("message", "data is null");

        }
        return map;
    }

    public static void main(String[] args) throws Exception {
//       String xx= AESUtils.Decrypt
//       ("UDqGPjg7jdn93NnPmjfh199e36xNX7lCwjQaXxRnGaPaxJAyEqiLMvesNOiUKZlST8UUGhc5VKZblM5P" +
//                "/kP6IsJdPoMuJGNHHmYi59IK7Jj2T+m5YcJ8Y7C+ZKh3/HUGb7Z9b6LY2bGOkVlrep7QdC2kFRF27kdcKn7IP3Cz8ZF" +
//                "+BOyLtx5Yfh6b1vcvoEJPWHoAIRqAviXfHmNNmEzwW1Fn5b8lG7F+UaBQhqDqWtUs7yKtV1zP+nzzsHUlI5IBynmOSQW9R
//                /OIoAbfMSF/Dw==","kzhtybx8qzmjrc06");
//        JSONObject jsonObject=JSONObject.parseObject(xx);
//         Customer customer= jsonObject.toJavaObject(Customer.class);
//        System.out.println(customer);

        boolean isChange = true;
        // 长沙，益阳，常德 ，娄底，邵阳，怀化，郴州，永州，湘西，张家界
        String[] cityNames = {"岳阳", "衡阳", "株洲", "湘潭"};
        for (String cityName : cityNames) {
            if ("永州".contains(cityName)) {
                isChange = false;
                break;
            }
        }
        if (isChange) {
            System.out.println(isChange);
        }

    }

    /**
     * 吉客
     *
     * @param customer
     * @return
     */
    @Log(title = "吉客客户信息准入查询", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/customerIsCheck", method = RequestMethod.POST)
    @ResponseBody
    public Map customerIsCheck(@RequestBody Customer customer) {
        Map<String, Object> map = new HashMap<>();
        map.put("dealStatus", "0000");
        return map;
    }

    /**
     * 吉客
     *
     * @param
     * @return
     */
    @Log(title = "吉客客户信息推送", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/customerJiKeInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map customerJiKeInsert(@RequestBody JSONObject params) {
        Map<String, Object> map = new HashMap<>();
        String aseKey = "Mi0FDuAaVtuRQFEA";
        String merchantNo = "H8319087593156";// 商户号
        if (!merchantNo.equals(params.getString("merchantNo"))) {
            map.put("dealStatus", "9999");
            map.put("dealDesc", "商户号错误");
            return map;
        }
        String data = params.getString("data");// 加密的串

        String keyAes = "90e9cd2477de4bd1bea80cf649d76ac8";
        try {
            // 解密
            data = Base64Util.decrypt(data, keyAes);
        } catch (Exception e) {
            map.put("dealStatus", "9999");
            map.put("dealDesc", "data解密失败");
            return map;
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (StringUtils.isNull(jsonObject)) {
            map.put("dealStatus", "9999");
            map.put("dealDesc", "data解密数据为空");
            return map;
        }
        if (null != jsonObject) {
            Customer customer = new Customer();
            customer.setLaiyuan("吉客");
            Optional.ofNullable(jsonObject.getString("applyId")).ifPresent(v -> {
                customer.setClueId(jsonObject.getString("applyId"));
            });
            Optional.ofNullable(jsonObject.getString("custName")).ifPresent(v -> {
                customer.setCusName(jsonObject.getString("custName"));
            });
            Optional.ofNullable(jsonObject.getString("city")).ifPresent(v -> {
                customer.setCityName(jsonObject.getString("city"));
            });
            Optional.ofNullable(jsonObject.getString("phoneNo")).ifPresent(v -> {
                customer.setPhone(jsonObject.getString("phoneNo"));
            });
            Optional.ofNullable(jsonObject.getString("sex")).ifPresent(v -> {
                switch (jsonObject.getString("sex")) {
                    case "1":
                        customer.setSex("男");
                        break;
                    case "2":
                        customer.setSex("女");
                        break;
                    default:
                        customer.setSex("未知");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("age")).ifPresent(v -> {
                customer.setAge(Integer.parseInt(jsonObject.getString("age")));
            });
            Optional.ofNullable(jsonObject.getString("loanAmount")).ifPresent(v -> {
                switch (jsonObject.getString("loanAmount")) {
                    case "1":
                        customer.setAmount("0-3万");
                        break;
                    case "2":
                        customer.setAmount("3-5万");
                        break;
                    case "3":
                        customer.setAmount("5-10万");
                        break;
                    case "4":
                        customer.setAmount("10-20万");
                        break;
                    case "5":
                        customer.setAmount("20万以上");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("profession")).ifPresent(v -> {
                switch (jsonObject.getString("profession")) {
                    case "1":
                        customer.setWorks("自由职业");
                        break;
                    case "2":
                        customer.setWorks("上班族");
                        break;
                    case "3":
                        customer.setWorks("个体户");
                        break;
                    case "4":
                        customer.setWorks("企业户");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("salaryType")).ifPresent(v -> {
                switch (jsonObject.getString("salaryType")) {
                    case "1":
                        customer.setRemark(customer.getRemark() + "/工资：现金");
                        break;
                    case "2":

                        customer.setRemark(customer.getRemark() + "/工资：打卡工资");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("socialSecurity")).ifPresent(v -> {
                switch (jsonObject.getString("socialSecurity")) {
                    case "1":
                        customer.setSocial("6个月下");
                        break;
                    case "2":
                        customer.setSocial("6个月上");
                        break;
                    case "0":
                        customer.setSocial("无");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("providentFund")).ifPresent(v -> {
                switch (jsonObject.getString("providentFund")) {
                    case "1":
                        customer.setAccumulation("6个月下");
                        break;
                    case "2":
                        customer.setAccumulation("6个月上");
                        break;
                    case "0":
                        customer.setAccumulation("无");
                        break;
                }
            });

            Optional.ofNullable(jsonObject.getString("car")).ifPresent(v -> {
                switch (jsonObject.getString("car")) {
                    case "1":
                        customer.setCarStatus("有按揭车");
                        break;
                    case "2":
                        customer.setCarStatus("有全款车");
                        break;
                    case "0":
                        customer.setCarStatus("无车");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("estate")).ifPresent(v -> {
                switch (jsonObject.getString("estate")) {
                    case "1":
                        customer.setHouseType("有按揭商品房");
                        break;
                    case "2":
                        customer.setHouseType("有全款商品房");
                        break;
                    case "0":
                        customer.setHouseType("无车");
                        break;
                }
            });
            Optional.ofNullable(jsonObject.getString("creditCardLimit")).ifPresent(v -> {
                String card = "";
                switch (jsonObject.getString("creditCardLimit")) {
                    case "1":
                        card = "3000以下";
                        break;
                    case "2":
                        card = ("3000-5000元");
                        break;
                    case "3":
                        card = ("5000-10000元");
                        break;
                    case "4":
                        card = ("10000元以上");
                        break;
                    case "0":
                        card = ("无信用卡");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/信用卡：" + card);
            });
            Optional.ofNullable(jsonObject.getString("zhiMaScore")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getString("zhiMaScore")) {
                    case "1":
                        zhima = ("600-650分");
                        break;
                    case "2":
                        zhima = ("650-700分");
                        break;
                    case "3":
                        zhima = ("700分以上");
                        break;
                    case "0":
                        zhima = ("无芝麻分");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/芝麻分：" + zhima);
            });
            Optional.ofNullable(jsonObject.getString("lifeInsurance")).ifPresent(v -> {
                switch (jsonObject.getString("lifeInsurance")) {
                    case "1":
                        customer.setPolicy("有保单");
                        break;
                    case "0":
                        customer.setPolicy("无保单");
                        break;
                }
            });

            Optional.ofNullable(jsonObject.getString("carNum")).ifPresent(v -> {
                customer.setCarNo(jsonObject.getString("carNum"));
            });

            Optional.ofNullable(jsonObject.getString("loanPurpose")).ifPresent(v -> {
                String use = "";
                switch (jsonObject.getString("loanPurpose")) {
                    case "1":
                        use = ("日常消费");
                        break;
                    case "2":
                        use = ("装修");
                        break;
                    case "3":
                        use = ("旅游");
                        break;
                    case "4":
                        use = ("教育培训");
                        break;
                    case "5":
                        use = ("婚庆");
                }
                customer.setRemark(customer.getRemark() + "/用途：" + use);
            });

            Optional.ofNullable(jsonObject.getString("carAge")).ifPresent(v -> {
                switch (jsonObject.getString("carAge")) {
                    case "1":
                        customer.setCarAge("10年以内");
                        break;
                    case "2":
                        customer.setCarAge("10年以上");
                        break;
                }
            });

            Optional.ofNullable(jsonObject.getString(" carCost")).ifPresent(v -> {
                switch (jsonObject.getString(" carCost")) {
                    case "1":
                        customer.setPgPrice("3万以下");
                        break;
                    case "2":
                        customer.setCarAge("3-10万");
                        break;
                    case "3":
                        customer.setCarAge("10万以上");
                        break;
                }
            });
            customer.setCityName("长沙市");
            Map maps = customerService.savePushCustomer(customer);
            if (maps.get("code").equals("200")) {
                map.put("dealStatus", "0000");
            } else {
                map.put("dealStatus", "9999");
                map.put("dealDesc", maps.get("message"));
            }
        }
        return map;
    }

    /**
     * 融薪花
     *
     * @param
     * @return
     */
    @Log(title = "融薪花客户信息推送", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/customerRongXinHua", method = RequestMethod.POST)
    @ResponseBody
    public Map customerRongXinHua(@RequestBody JSONObject params) {
        Map<String, Object> map = new HashMap<>();
        String aseKey = "Mi0FDuAaVtuRQFEA";
        String merchantNo = "H8319087593156";// 商户号
        if (!params.getString("channel").equals(merchantNo)) {
            map.put("code", "500");
            map.put("msg", "商户号错误");// 商户号错误
            return map;
        }
        if (!params.getString("action").equals("apply")) {
            map.put("code", "500");
            map.put("msg", "业务逻辑错误");// 商户号错误
            return map;
        }
        if (StringUtils.isEmpty(params.getString("data"))) {
            map.put("code", "500");
            map.put("msg", "业务数据为空");// 商户号错误
            return map;
        }

        try {
            String data = params.getString("data");
            JSONObject jsonObject = JSONObject.parseObject(AESUtils.Decrypt(data, aseKey));
            Customer customer = new Customer();
            customer.setLaiyuan("融薪花");
            Optional.ofNullable(jsonObject.getString("phone")).ifPresent(v -> {
                customer.setPhone(jsonObject.getString("phone"));
            });
            Optional.ofNullable(jsonObject.getString("name")).ifPresent(v -> {
                customer.setCusName(jsonObject.getString("name"));
            });
            Optional.ofNullable(jsonObject.getInteger("age")).ifPresent(v -> {
                customer.setAge(jsonObject.getInteger("age"));
            });
            Optional.ofNullable(jsonObject.getString("id_num")).ifPresent(v -> {
                customer.setIdCard(jsonObject.getString("id_num"));
            });
            Optional.ofNullable(jsonObject.getString("city")).ifPresent(v -> {
                customer.setCityName(jsonObject.getString("city"));
            });
            Optional.ofNullable(jsonObject.getInteger("loan")).ifPresent(v -> {
                customer.setAmount(jsonObject.getInteger("loan") + "");
            });
            Optional.ofNullable(jsonObject.getString("joinip")).ifPresent(v -> {
                customer.setIp(jsonObject.getString("joinip"));
            });
            Optional.ofNullable(jsonObject.getInteger("zhima")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("zhima")) {
                    case 3:
                        zhima = ("600-650分");
                        break;
                    case 4:
                        zhima = ("650-700分");
                        break;
                    case 5:
                        zhima = ("700分以上");
                        break;
                    case 1:
                        zhima = ("无芝麻分");
                        break;
                    case 2:
                        zhima = ("600分以下");
                        break;
                }
                customer.setRemark("芝麻分：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("huabei")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("huabei")) {
                    case 1:
                        zhima = ("无花呗");
                        break;
                    case 2:
                        zhima = ("5000以下");
                        break;
                    case 3:
                        zhima = ("5000-10000");
                        break;
                    case 4:
                        zhima = ("10000以上");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/花呗：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("baitiao")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("baitiao")) {
                    case 1:
                        zhima = ("无白条");
                        break;
                    case 2:
                        zhima = ("5000以下");
                        break;
                    case 3:
                        zhima = ("5000以上");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/白条：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("fund")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("fund")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;

                }
                customer.setRemark(customer.getRemark() + "/公积金：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("has")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("has")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;

                }
                customer.setRemark(customer.getRemark() + "/社保：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("house")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("house")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/房产：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("car")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("car")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/车产：" + zhima);
            });

            Optional.ofNullable(jsonObject.getInteger("enterprise")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("enterprise")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/营业执照：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("insurance")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("insurance")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/保单：" + zhima);
            });
            Optional.ofNullable(jsonObject.getInteger("overdue")).ifPresent(v -> {
                String zhima = "";
                switch (jsonObject.getInteger("overdue")) {
                    case 1:
                        zhima = ("无");
                        break;
                    case 2:
                        zhima = ("有");
                        break;
                }
                customer.setRemark(customer.getRemark() + "/逾期：" + zhima);
            });
            Optional.ofNullable(jsonObject.getString("property")).ifPresent(v -> {
                customer.setRemark(customer.getRemark() + "/其他：" + jsonObject.getString("property"));
            });
            Map maps = customerService.savePushCustomer(customer);
            if (maps.get("code").equals("200")) {
                map.put("code", "200");
                map.put("msg", maps.get("message"));
                map.put("data", customer);
            } else {
                map.put("code", "500");
                map.put("msg", maps.get("message"));
                map.put("data", customer);
            }
            return map;
        } catch (Exception e) {
            map.put("code", "500");
            map.put("msg", "解密错误");
            return map;
        }


    }

    /**
     * 21世纪金融
     *
     * @param
     * @return
     */
    @Log(title = "21世纪金融客户信息推送", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/customerTwentyOneFinanceInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map customerRongXinHua(@RequestBody Customer21Vo customer21Vo) {
        Map<String, Object> map = new HashMap<>();
        Customer customer = new Customer();
        customer.setLaiyuan("21世纪金融");
        customer.setClueId(customer21Vo.getClientNo());
        customer.setCusName(customer21Vo.getClientName());
        customer.setPhone(customer21Vo.getMobile());
        customer.setCityName(customer21Vo.getCity().replace("市", ""));
        customer.setRemark(customer21Vo.getProvince() + ";" + customer21Vo.getCity() + ";" + customer21Vo.getDistrict());
        String remark = "";
        for (Customer21ListVo v : customer21Vo.getContent()) {
            remark = remark + v.getLabel() + "：" + v.getValue() + ";";
        }
        customer.setRemark(customer.getRemark() + remark);
        Map maps = customerService.savePushCustomer(customer);
        if (maps.get("code").equals("200")) {
            map.put("code", 200);
            map.put("msg", maps.get("message"));
        } else {
            map.put("code", 500);
            map.put("msg", maps.get("message"));
        }
        return map;
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "群德客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerQunDe", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerQunDe(@RequestBody Customer customer) {
        String city = customer.getCityName();
        String citys[] = city.split("-");
        if (citys.length == 2) {
            customer.setCityName(citys[1].replace("市", ""));
        } else {
            customer.setCityName(city.replace("市", ""));
        }
        return customerService.savePushCustomer(customer);
    }

    /**
     * 已接通 10 通用
     *
     * @param customer
     * @return
     */
    @Log(title = "群德拒量QD客户推送信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerQunDeSecond", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerQunDeSecond(@RequestBody Customer customer) {
        String city = customer.getCityName();
        customer.setLaiyuan("拒量QD");
        String citys[] = city.split("-");
        String oldCity = "";
        if (citys.length == 2) {
            oldCity = citys[1].replace("市", "");
        } else {
            oldCity = city.replace("市", "");
        }
        customer.setCityName("长沙");
        customer.setRemark(customer.getRemark() + "/原城市：" + oldCity);
        return customerService.savePushCustomer(customer);
    }


    /**
     * 融360 撞库 株洲
     *
     * @param
     * @return
     */
    @Log(title = "融360撞库", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertCustomerByRong360IsCheckByZhuZhou", method = RequestMethod.POST)
    @ResponseBody
    public Map insertCustomerByRong360IsCheck(String channel_code, String mobile, String sign, String timestamp) throws Exception {
        Map map = new HashMap();
        if (StringUtils.isNotEmpty(channel_code) && "rong360".equals(channel_code)) {
            String phone = new String(Rong360Codec.base64StrDecode("0wqu3DsnDlG3lqlA", "TgAB9uLpKUXJIyka", mobile));
            phone = phone.substring(0, 7);
            Customer customer = new Customer();
            customer.setPhone(phone);
            List<Customer> customers = customerService.findList(customer);
            if (customers.size() > 0) {
                List<String> list = new ArrayList<>();
                for (Customer cus : customers) {
                    list.add(Md5Utils.hash(cus.getPhone()));
                }
                map.put("code", 1);
                map.put("data", list);
                map.put("msg", "success");
            } else {
                map.put("code", 0);
                map.put("data", null);
                map.put("msg", "fail");
            }

        } else {
            map.put("msg", "channel_code is error");
            map.put("code", -1);
        }
        return map;
    }

    /**
     * 海通-客户进件
     * 手机号码
     *
     * @param
     * @return
     */
    @Log(title = "海通-客户进件", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/insertHaiTongCustomer", method = RequestMethod.POST)
    @ResponseBody
    public String insertHaiTongCustomer(@RequestBody JSONObject param) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(param.getString("data"))) {
            JSONObject jsonObject = JSONObject.parseObject(Objects.requireNonNull(AESUtils.Decrypt(param.getString(
                    "data"), "UmhX3f2Ej6I9kq15")));
            if (StringUtils.isNotNull(jsonObject)) {
                Customer customer = new Customer();
                customer.setLaiyuan("海通");
                Optional.ofNullable(jsonObject.getString("phone")).ifPresent(customer::setPhone);
                Optional.ofNullable(jsonObject.getString("name")).ifPresent(customer::setCusName);

                Optional.ofNullable(jsonObject.getString("city")).ifPresent(v -> {
                    customer.setCityName("长沙");
                    customer.setRemark("原城市：" + v);
                });
                Optional.ofNullable(jsonObject.getInteger("age")).ifPresent(customer::setAge);
                Optional.ofNullable(jsonObject.getInteger("gender")).ifPresent(v -> {
                    String content = "";
                    switch (v) {
                        case 0:
                            content = "未知";
                            break;
                        case 1:
                            content = "男";
                            break;
                        case 2:
                            content = "女";
                            break;
                    }
                    customer.setSex(content);
                });

                Optional.ofNullable(jsonObject.getString("car_value")).ifPresent(customer::setCarNo);
                Optional.ofNullable(jsonObject.getString("id")).ifPresent(customer::setClueId);
                customerService.savePushCustomer(customer);
            }
        } else {
            return "fail";
        }

        return "success";
    }


}


